package com.budwk.app.sqfwV2.generator.shawenTown.pdf;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.component.ShawenTownComponent;
import com.budwk.app.sqfwV2.dto.shawenTown.SWTownProblemVo;
import com.budwk.app.sqfwV2.dto.shawenTown.ShawenTownSafetyPdfDTO;
import com.budwk.app.sqfwV2.dto.shawenTown.ShawenTownSafetyProblemVo;
import com.budwk.app.util.poiUtils.SitePictureOfficeUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 图片预览 生成pdf 2.0后操作
 */

@Slf4j
public class ShawenPictureGenerator extends BasePdfGenerator {

    @Override
    protected boolean pdfCreateConditionCheck(PdfGenerateParam pdfGenerateParam) {
        return isGenerate(pdfGenerateParam);
    }

    // 檢查是否有隐患图片
    private boolean isGenerate(PdfGenerateParam pdfGenerateParam) {
        ShawenTownSafetyPdfDTO pdfDTO = ShawenTownComponent.getShawenTownSafetyPdfDTO(pdfGenerateParam);
        if (StringUtils.isNotEmpty(pdfDTO.getVisitPictures())) {
            return true;
        }
        // 其他隐患图片
        List<SWTownProblemVo> problems = getProblems(pdfDTO);
        Optional<SWTownProblemVo> optional = problems.stream().filter(e -> StringUtils.isNotEmpty(e.getRiskImages()) || StringUtils.isNotEmpty(e.getRiskImages())).findAny();
        return optional.isPresent();
    }


    private List<SWTownProblemVo> getProblems(ShawenTownSafetyPdfDTO pdfDTO) {
        List<SWTownProblemVo> problemVos = new ArrayList<>();
        ShawenTownSafetyProblemVo small = pdfDTO.getSmallBusinessEstablishment();
        addAllImages(problemVos, small.getParams());
        addAllImages(problemVos, small.getOtherDangers());
        if (pdfDTO.getElectricBicycle() != null) {
            addAllImages(problemVos, pdfDTO.getElectricBicycle().getParams());
        }
        return problemVos;
    }

    @Override
    protected void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList = Lists.newArrayList();
        ShawenTownSafetyPdfDTO pdfDTO = ShawenTownComponent.getShawenTownSafetyPdfDTO(pdfGenerateParam);
        String visitPictures = pdfDTO.getVisitPictures();
        if (StringUtils.isNotBlank(visitPictures)) {
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(visitPictures.split(",")));
            String textStr = "附件：{} {} 走访照片";
            textStr = StrFormatter.format(textStr, pdfDTO.getAddress(), DateUtil.format(pdfDTO.getFillDate(), "yyyy-MM-dd"));
            sitePictureContentList.add(new SitePictureOfficeUtils.SitePictureContent(textStr, pictureList));
        }
        List<SWTownProblemVo> problemVos = getProblems(pdfDTO);
        // 所有的隐患图片
        List<String> yhImage = problemVos.stream().map(SWTownProblemVo::getRiskImages).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        String textStr = "附件：{} 隐患图片";
        buildImage(textStr, yhImage, pdfDTO, sitePictureContentList);
        // 所有的整改图片
        List<String> zgImage = problemVos.stream().map(SWTownProblemVo::getRectificationAfterPhoto).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        String textStr1 = "附件：{} 整改图片";
        buildImage(textStr1, zgImage, pdfDTO, sitePictureContentList);
        Map<String, Object> entityMap = SitePictureOfficeUtils.popularParamMap(sitePictureContentList);
        if (entityMap != null) {
            paramMap.putAll(entityMap);
        }
    }

    private void buildImage(String textStr, List<String> images, ShawenTownSafetyPdfDTO pdfDTO, List<SitePictureOfficeUtils.SitePictureContent> sitePictureContentList) {
        for (int i = 0; i < images.size(); i++) {
            String imageUrl = images.get(i);
            if (i == 0) {
                textStr = StrFormatter.format(textStr, DateUtil.format(pdfDTO.getFillDate(), "yyyy-MM-dd"));
            } else {
                textStr = StringUtils.EMPTY;
            }
            List<String> pictureList = Lists.newArrayList(Lists.newArrayList(imageUrl.split(",")));
            sitePictureContentList.add(new SitePictureOfficeUtils.SitePictureContent(textStr, pictureList));
        }
    }

    private void addAllImages(List<SWTownProblemVo> problemVos, List<SWTownProblemVo> params) {
        if (params != null && params.size() > 0) {
            problemVos.addAll(params);
        }
    }

    @Override
    protected String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return CommonConstants.PDF_NULL_TEMPLATE_PATH;
    }
}
